/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package be.ugent.medirem.persistence.util;

import be.ugent.medirem.persistence.entity.Consultation;
import be.ugent.medirem.persistence.entity.GeneralPractitioner;
import be.ugent.medirem.persistence.entity.Location;
import be.ugent.medirem.persistence.entity.Medication;
import be.ugent.medirem.persistence.entity.MedicationTask;
import be.ugent.medirem.persistence.entity.Patient;
import be.ugent.medirem.persistence.entity.Prescription;
import be.ugent.medirem.persistence.entity.details.ConsultationDetails;
import be.ugent.medirem.persistence.entity.details.GpDetails;
import be.ugent.medirem.persistence.entity.details.LocationDetails;
import be.ugent.medirem.persistence.entity.details.MedicationDetails;
import be.ugent.medirem.persistence.entity.details.MedicationTaskDetails;
import be.ugent.medirem.persistence.entity.details.PatientDetails;
import be.ugent.medirem.persistence.entity.details.PrescriptionDetails;
import be.ugent.medirem.persistence.entity.values.GpValues;
import be.ugent.medirem.persistence.entity.values.LocationValues;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Simon
 */
public class EntityDetailsToEntity {

    public static MedicationDetails medicationToDetails(Medication m) {
        return new MedicationDetails(m.getId(), m.getName(), m.getStdDose(), m.getUnitDose().getName(), m.getMeasurementType() == null ? null : m.getMeasurementType().getName());
    }

    public static PrescriptionDetails prescriptionToDetails(Prescription p) {
        return new PrescriptionDetails(p.getId(), medicationToDetails(p.getMedication()), gPToDetails(p.getGp()), p.getFrequency(),
                p.getDosage().getDosage(), p.getDosage().isIsFixed(), p.getDosage().getUnit().getName(), p.getStartTime(), p.getEndTime(), p.getRemarks());
    }

    public static MedicationTaskDetails medicationTaskToDetails(MedicationTask m) {
        return new MedicationTaskDetails(m.getId(), prescriptionToDetails(m.getPrescription()), m.getStatus(), m.getTaskTime(), m.getCompletionTime());
    }

    public static GeneralPractitioner detailsToGP(GpValues gp, String password) {
        GeneralPractitioner g = new GeneralPractitioner(gp.getUsername(), password, gp.getFirstname(), gp.getSurname(), null);
        g.setId(gp.getId());
        return g;
    }

    public static GpDetails gPToDetails(GeneralPractitioner gp) {
        Collection<PatientDetails> list = new ArrayList<PatientDetails>();
        for (Patient p : gp.getPatients())
            list.add(patientToDetails(p));
        GpDetails g = new GpDetails(gp.getUsername(), gp.getFirstname(), gp.getSurname(), list, gp.getLocation() != null ? gp.getLocation().getId() : null);
        return g;
    }

    public static Location detailsToLocation(LocationValues l) {
        return new Location(l.getStreet(), l.getNumber(), l.getZipcode(), l.getTown(), l.getCountry(), l.getPhoneNumber(), l.getEmail());
    }

    public static LocationDetails locationToDetails(Location l) {
        return new LocationDetails(l.getId(), l.getStreet(), l.getNumber(), l.getZipcode(), l.getTown(), l.getCountry(), l.getPhonenr(), l.getEmail());
    }

    public static PatientDetails patientToDetails(Patient p) {
        return new PatientDetails(p.getId(), p.getUsername(), p.getFirstname(),
                p.getSurname(), p.getBirthdate(), p.getBloodgroup(), p.getSocialSecurityNr(),
                p.getLocation()!=null?p.getLocation().getId():null);
    }

    public static ConsultationDetails detailsToConsultation(Consultation c) {
        return new ConsultationDetails(c.getId(), c.getPatient().getId(), c.getGp().getId(), c.getDate());
    }

}
